# -*- coding: utf-8 -*-
# !/usr/bin/env python
import uuid
from rest_framework.views import APIView
from components.models import ConfManagement
from components.resource_management.conf_manage.dao import query_conf
from utils.views import Result

class ConfManage(APIView):
    def get(self, request):
        data = request.query_params
        conf_data_list = []
        if data['conf_manage_id']:
            conf_data_list = query_conf(data['conf_manage_id'])
        return Result.success_response(data=conf_data_list, msg='查询成功')

    def post(self, request):
        data = request.data
        if 'conf_manage_id' in data: data.pop('conf_manage_id')
        data['application'] = 'conf'
        data['status'] = 1
        try:
            save_data = ConfManagement(**data)
            save_data.save()
        except Exception as e:
            return Result.fail_response(msg=str(e))

        return Result.success_response(msg='保存成功')


    def put(self, request):
        data = request.data.copy()

        ConfManagement.objects.filter(conf_manage_id=data['conf_manage_id']).update(
            conf_manage_nick=data['conf_manage_nick'],
            conf_manage_name=data['conf_manage_name'],
            conf_manage_path=data['conf_manage_path'],
            conf_manage_code=data['conf_manage_code'],
            conf_manage_content=data['conf_manage_content'],
            conf_manage_comment=data['conf_manage_comment']
        )
        return Result.success_response(msg='修改成功')


    def delete(self, request):
        data = request.data
        conf_manage_id_list = data['conf_manage_id_list']

        ConfManagement.objects.filter(conf_manage_id__in=conf_manage_id_list).update(status=0)

        return Result.success_response(msg='删除成功')